﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Windows.Forms;

namespace DemoCode
{
    public partial class msmq : Form
    {
        public msmq()
        {
            InitializeComponent();
        }
        string queuePath = @".\Private$\mymqtest";
        //发送
        private void button1_Click(object sender, EventArgs e)
        {
            //EnsureQueueExists(queuePath);
            //var ipAddress = "192.168.134.230";
            //var mqName = "gtr_dnf_income";


            var ipAddress = "192.168.1.185";
            var mqName = "gtr_dnf_income";
            var path = string.Format(@"FormatName:DIRECT=TCP:{0}\Private$\{1}", ipAddress, mqName);
            MsmqMessage msgEntity = new MsmqMessage();
            msgEntity.BuyerActor = "￡鬼魅☆残影￡";
            msgEntity.GameId = 46;
            msgEntity.NO = "11111111111111111111";
            msgEntity.CreateTime = DateTime.Now;
            msgEntity.IsTransact = true;
            msgEntity.IsTelecom = true;
            msgEntity.IsFinished = false;
            msgEntity.TradeItemsID = "游戏币.金";
            msgEntity.BusinessType = 1;
            msgEntity.ProxyCity = "中国";
            msgEntity.ProxyAnable = false;
            msgEntity.SellerSex = 0;
            msgEntity.SellerProfession = "£鬼魅☆残影£";
            msgEntity.SellerCamp = -1;
            msgEntity.SellerAccount = "￡鬼魅☆残影￡";
            msgEntity.SellerPassword = "rvfL6SVJGaon3D2wob7hqA==";
            msgEntity.SellerSecondPassword = "£鬼魅☆残影£";
            msgEntity.SellerArea = "电信区";
            msgEntity.SellerServer = "悬铃山崖/冰晶雪原/翡翠树海/烟雾迷森";
            msgEntity.SellerActor = "酱油缘";
            msgEntity.SellerLevel = -1;
            msgEntity.Direction = 0;
            msgEntity.GameMatrix = new List<List<string>>() { new List<string>() { "0" } };
            msgEntity.SellerMbkImage = new byte[] { };
            msgEntity.SellMoney = 1;
            msgEntity.SellerIndexNO = 5173;
            msgEntity.SellerGoldTradePassword = "电信区";
            msgEntity.SellerActorPassword = "e2eWoa0eELM=";
            msgEntity.BuyerLevel = 0;
            msgEntity.BuyMoney = 1;
            msgEntity.BuyerActorDigitID = "电信区";
            //£  -> &pound;
            // > -> &glt;

            System.Reflection.Assembly ass = System.Reflection.Assembly.Load("System.Messaging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL");
            dynamic mqObj = ass.CreateInstance("System.Messaging.MessageQueue");
            dynamic xmlFormat = ass.CreateInstance("System.Messaging.XmlMessageFormatter");
            xmlFormat.TargetTypes = new Type[] { typeof(MsmqMessage) };
            mqObj.Formatter = xmlFormat;
            mqObj.Path = path;
            dynamic mqE = ass.CreateInstance("System.Messaging.Message");
            mqE.Recoverable = true;
            mqE.Recoverable = msgEntity;
            mqObj.Send(mqE);

            using (System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue(path))
            {
                mq.Path = "";
                System.Messaging.XmlMessageFormatter xf = new System.Messaging.XmlMessageFormatter();
                xf.TargetTypes = new Type[] { };

                mq.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(MsmqMessage) });
                System.Messaging.Message msg = new System.Messaging.Message();
                msg.Recoverable = true;
                msg.Body = msgEntity;
                try
                {
                    mq.Send(msg);
                }
                catch (Exception ex)
                {
                }
            }
        }

        // Creates the queue if it does not already exist.
        public static void EnsureQueueExists(string path)
        {
            if (!MessageQueue.Exists(path))
            {
                MessageQueue.Create(path);
            }
        }
        //接收
        private void button2_Click(object sender, EventArgs e)
        {
            using (MessageQueue mq = new MessageQueue(queuePath))
            {
                mq.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(Order) });
                Order newOrder = (Order)mq.Receive().Body;
                newOrder.ShipItems();
            }

        }

    }
    public class Order
    {

        public int itemId;
        public int quantity;
        public string address;

        public void ShipItems()
        {

            Console.WriteLine("Order Placed:");
            Console.WriteLine("\tItem ID  : {0}", itemId);
            Console.WriteLine("\tQuantity : {0}", quantity);
            Console.WriteLine("\tShip To  : {0}", address);

            // Add order to the database.
            /* Insert code here. */

        }
    }
    [Serializable]
    public class MsmqMessage
    {

        private string _no;
        private string _sellerAccount;
        private string _sellerPassword;
        private string _sellerArea;
        private string _sellerServer;
        private string _sellerActor;
        private UInt64 _sellMoney;
        private Int32 _sellerLevel;
        private byte[] _sellerMbkImage;
        private DateTime _createTime;
        private bool _isTransact;
        private string _buyerActor;
        private UInt64 _buyMoney;
        private List<List<string>> _gameMatrix;
        private int _direction;
        private int _gameId;
        private bool _isTelecom;
        private bool _isFinished;
        private string _sellerSecondPassword;
        private Int32 _buyerLevel;
        private Int32 _sellerIndexNO = -1;
        private Int32 _sellerCamp;
        private string _sellerGoldTradePassword;
        private string _tradeItemsID;
        private string _sellerActorPassword;
        private Int32 _businessType;
        private string _sendToMsmqIP;
        private string _proxyCity;
        private bool _proxyAnable;
        private string _buyerActorDigitID;
        private string _sellerProfession;
        private Int32 _sellerSex;

        /// <summary>
        /// 主站记录发送到消息队列的IP
        /// </summary>
        public string SendToMsmqIP
        {
            get
            {
                return _sendToMsmqIP;
            }
            set
            {
                _sendToMsmqIP = value;
            }
        }

        #region Common Info.
        public Int32 GameId
        {
            get
            {
                return _gameId;
            }
            set
            {
                _gameId = value;
            }
        }

        /// <summary>
        /// Transact code.
        /// </summary>
        public String NO
        {
            get
            {
                return _no;
            }
            set
            {
                _no = value;
            }
        }

        /// <summary>
        /// Create time.
        /// </summary>
        public DateTime CreateTime
        {
            get
            {
                return _createTime;
            }
            set
            {
                _createTime = value;
            }
        }


        /// <summary>
        /// 0：纵向数字，横向字母
        /// 1：横向数字，纵向字母
        /// </summary>
        public int Direction
        {
            get
            {
                return _direction;
            }
            set
            {
                _direction = value;
            }
        }

        /// <summary>
        /// False: Do verify.
        /// True: Do transact.
        /// </summary>
        public Boolean IsTransact
        {
            get
            {
                return _isTransact;
            }
            set
            {
                _isTransact = value;
            }
        }

        public bool IsTelecom
        {
            get
            {
                return _isTelecom;
            }
            set
            {
                _isTelecom = value;
            }

        }

        public bool IsFinished
        {
            get
            {
                return _isFinished;
            }
            set
            {
                _isFinished = value;
            }

        }

        public List<List<string>> GameMatrix
        {
            get
            {
                return _gameMatrix;
            }
            set
            {
                _gameMatrix = value;
            }

        }
        /// <summary>
        /// 交易物品ID
        /// </summary>
        public string TradeItemsID
        {
            get { return _tradeItemsID; }
            set { _tradeItemsID = value; }
        }

        /// <summary>
        /// 1 金币交易单
        /// 2 金币发布单
        /// 3 帐号业务审核单
        /// 4 帐号业务交易单
        /// </summary>
        public Int32 BusinessType
        {
            get { return _businessType; }
            set { _businessType = value; }
        }

        /// <summary>
        /// IP所在地区
        /// </summary>
        public string ProxyCity
        {
            get { return _proxyCity; }
            set { _proxyCity = value; }
        }

        /// <summary>
        /// 区服设置是否启用安全模式开关
        /// </summary>
        public bool ProxyAnable
        {
            get { return _proxyAnable; }
            set { _proxyAnable = value; }
        }
        #endregion

        #region Seller Info.
        /// <summary>
        /// 性别
        /// </summary>
        public Int32 SellerSex
        {
            get { return _sellerSex; }
            set { _sellerSex = value; }
        }
        /// <summary>
        /// 职业
        /// </summary>
        public string SellerProfession
        {
            get { return _sellerProfession; }
            set { _sellerProfession = value; }
        }
        /// <summary>
        /// seller actor camp 卖家阵营
        /// </summary>
        public Int32 SellerCamp
        {
            get { return _sellerCamp; }
            set { _sellerCamp = value; }
        }
        /// <summary>
        /// Seller account.
        /// </summary>
        public String SellerAccount
        {
            get
            {
                return _sellerAccount;
            }
            set
            {
                _sellerAccount = value;
            }
        }

        /// <summary>
        /// Seller password.
        /// </summary>
        public String SellerPassword
        {
            get
            {
                return _sellerPassword;
            }
            set
            {
                _sellerPassword = value;
            }
        }

        public string SellerSecondPassword
        {
            get
            {
                return _sellerSecondPassword;
            }
            set
            {
                _sellerSecondPassword = value;
            }
        }

        /// <summary>
        /// Actor area.
        /// </summary>
        public String SellerArea
        {
            get
            {
                return _sellerArea;
            }
            set
            {
                _sellerArea = value;
            }
        }
        /// <summary>
        /// Actor server.
        /// </summary>
        public String SellerServer
        {
            get
            {
                return _sellerServer;
            }
            set
            {
                _sellerServer = value;
            }
        }
        /// <summary>
        /// Seller actor name.
        /// </summary>
        public String SellerActor
        {
            get
            {
                return _sellerActor;
            }
            set
            {
                _sellerActor = value;
            }
        }
        /// <summary>
        /// Seller actor level.
        /// </summary>
        public Int32 SellerLevel
        {
            get
            {
                return _sellerLevel;
            }
            set
            {
                _sellerLevel = value;
            }
        }
        /// <summary>
        /// Password protector card image.
        /// </summary>
        public Byte[] SellerMbkImage
        {
            get
            {
                return _sellerMbkImage;
            }
            set
            {
                _sellerMbkImage = value;
            }
        }
        /// <summary>
        /// Sell money number.
        /// </summary>
        public UInt64 SellMoney
        {
            get
            {
                return _sellMoney;
            }
            set
            {
                _sellMoney = value;
            }
        }

        public Int32 SellerIndexNO
        {
            get
            {
                return _sellerIndexNO;
            }
            set
            {
                _sellerIndexNO = value;
            }

        }
        /// <summary>
        /// 该属性只为新武林外传的元宝交易密码
        /// </summary>
        public string SellerGoldTradePassword
        {
            get { return _sellerGoldTradePassword; }
            set { _sellerGoldTradePassword = value; }
        }
        /// <summary>
        /// QQ华夏的角色密码
        /// </summary>
        public string SellerActorPassword
        {
            get { return _sellerActorPassword; }
            set { _sellerActorPassword = value; }
        }
        #endregion

        #region Buyer Info.
        /// <summary>
        /// Seller actor name.
        /// </summary>
        public String BuyerActor
        {
            get
            {
                return _buyerActor;
            }
            set
            {
                _buyerActor = value;
            }
        }

        public Int32 BuyerLevel
        {
            get
            {
                return _buyerLevel;
            }
            set
            {
                _buyerLevel = value;
            }
        }

        /// <summary>
        /// 买价要买的金币数量
        /// </summary>
        public UInt64 BuyMoney
        {
            get
            {
                return _buyMoney;
            }
            set
            {
                _buyMoney = value;
            }
        }

        /// <summary>
        /// 角色名数字ID
        /// </summary>
        public string BuyerActorDigitID
        {
            get { return _buyerActorDigitID; }
            set { _buyerActorDigitID = value; }
        }
        #endregion
    }
}
